home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MACVOGL- / MAPPING.C < prev    next >
C/C++ Source or Header  |  1992-07-19  |  2KB  |  103 lines

  1. #include "vogl.h"
  2.  
  3. static    double    Vcx, Vcy, Vsx, Vsy;
  4. static    Matrix    msave;
  5.  
  6. /*
  7.  * VtoWxy
  8.  *
  9.  * Return the world x and y values corresponding to the input
  10.  * screen x and y values.
  11.  */
  12. void
  13. VtoWxy(xs, ys, xw, yw)
  14.     double    xs, ys;
  15.     double    *xw, *yw;
  16. {
  17.     double    d, a1, a2, b1, b2, c1, c2, A, B;
  18.  
  19.     A = (xs - Vcx) / Vsx;
  20.     B = (ys - Vcy) / Vsy;
  21.  
  22.     a1 = msave[0][0] - msave[0][3] * A;
  23.     a2 = msave[0][1] - msave[0][3] * B;
  24.  
  25.     b1 = msave[1][0] - msave[1][3] * A;
  26.     b2 = msave[1][1] - msave[1][3] * B;
  27.  
  28.     c1 = msave[3][3] * A - msave[3][0];
  29.     c2 = msave[3][3] * B - msave[3][1];
  30.  
  31.     d = (a2 * b1 - b2 * a1);
  32.  
  33.     if (d != 0.0) {
  34.         *xw = (b1 * c2 - c1 * b2) / d;
  35.         *yw = (c1 * a2 - a1 * c2) / d;
  36.     } else {
  37.         *xw = A;
  38.         *yw = B;
  39.     }
  40.  
  41.     if (*xw == 0.0)
  42.         *xw = A;
  43.  
  44.     if (*yw == 0.0)
  45.         *yw = B;
  46. }
  47.  
  48. /*
  49.  * _mapmsave
  50.  *
  51.  * saves the current transformation matrix as loaded by "loadmatrix" and
  52.  * before and other transformations (ie rotation and scaling) are
  53.  * concatenated with it.
  54.  */
  55. void
  56. _mapmsave(m)
  57.     Matrix    m;
  58. {
  59.     copymatrix(msave, m);
  60. }
  61.  
  62. /*
  63.  * calcW2Vcoeffs
  64.  *
  65.  *    Calculate the linear coeffs defining the mapping of world
  66.  *    space to actual device space
  67.  */
  68. void
  69. CalcW2Vcoeffs()
  70. {
  71.     Vcx = (double)(vdevice.maxVx + vdevice.minVx) * 0.5;
  72.     Vcy = (double)(vdevice.maxVy + vdevice.minVy) * 0.5;
  73.  
  74.     Vsx = (double)(vdevice.maxVx - vdevice.minVx) * 0.5;
  75.     Vsy = (double)(vdevice.maxVy - vdevice.minVy) * 0.5;
  76. }
  77.  
  78. /*
  79.  * WtoVx
  80.  *
  81.  * return the Screen X coordinate corresponding to world point 'p' 
  82.  * (does the perspective division as well)
  83.  */
  84. int
  85. WtoVx(p)
  86.     double    p[];
  87. {
  88.     return((int)(p[0] * Vsx / p[3] + Vcx + 0.5));
  89. }
  90.  
  91. /*
  92.  * WtoVy
  93.  *
  94.  * return the Screen Y coordinate corresponding to world point 'p' 
  95.  * (does the perspective division as well)
  96.  */
  97. int
  98. WtoVy(p)
  99.     double    p[];
  100. {
  101.     return((int)(p[1] * Vsy / p[3] + Vcy + 0.5));
  102. }
  103.